package com.size.mall.mallsearch.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.size.mall.mallapi.entity.PmsSearchSkuInfo;
import com.size.mall.mallapi.entity.PmsSkuInfo;
import com.size.mall.mallapi.service.ConvertDataToSearchService;
import com.size.mall.mallapi.service.PmsSkuInfoService;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@Service
public class ConvertDataToSearchServiceImpl implements ConvertDataToSearchService {

    @Reference
    PmsSkuInfoService pmsSkuInfoService;

    @Autowired
    JestClient jestClient;


    @Override
    public void ConvertData() throws InvocationTargetException, IllegalAccessException, IOException {

        List<PmsSkuInfo> pmsSkuInfos = pmsSkuInfoService.getAllSkuByCatalog3Id("61");
        List<PmsSearchSkuInfo> searchSkuInfos = CollUtil.newArrayList();
        for (PmsSkuInfo skuInfo : pmsSkuInfos) {

            PmsSearchSkuInfo pmsSearchSkuInfo = new PmsSearchSkuInfo();
            BeanUtil.copyProperties(skuInfo, pmsSearchSkuInfo);
            searchSkuInfos.add(pmsSearchSkuInfo);

        }


        // 构建索引
        for (PmsSearchSkuInfo skuInfo : searchSkuInfos) {
            Index index = new Index.Builder(skuInfo).index("mall").type("searchSku").build();

            jestClient.execute(index);
        }


    }


}
